﻿using System.Windows;
#if (WINRT)
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
#else
using System.Windows.Markup;
using System.Windows.Controls;
#endif

namespace nRoute.Controls
{
#if (SILVERLIGHT || WINRT)
    [TemplateVisualState(Name = NavigationContainer.UNNAVIGATED_STATE, GroupName = NavigationContainer.NAVIGATION_STATES)]
    [TemplateVisualState(Name = NavigationContainer.NAVIGATING_STATE, GroupName = NavigationContainer.NAVIGATION_STATES)]
    [TemplateVisualState(Name = NavigationContainer.NAVIGATED_STATE, GroupName = NavigationContainer.NAVIGATION_STATES)]
    [TemplateVisualState(Name = NavigationContainer.NAVIGATION_FAILED_STATE, GroupName = NavigationContainer.NAVIGATION_STATES)]
#endif
    [ContentProperty]
    public partial class NavigationContainer
		 : NavigationContentControl
    {
        protected internal const string UNNAVIGATED_STATE = "UnNavigated";                  // NavigationState.UnNavigated
        protected internal const string NAVIGATING_STATE = "Navigating";                     // NavigationState.Navigating
        protected internal const string NAVIGATED_STATE = "Navigated";                      // NavigationState.Navigated
        protected internal const string NAVIGATION_FAILED_STATE = "NavigationFailed";       // NavigationState.NavigationFailed
        protected internal const string NAVIGATION_STATES = "NavigationStates";

#region Initialize Related

        protected override void InitializeComponent()
        {
            // call the base 
            base.InitializeComponent();

            // we set the default style
            this.DefaultStyleKey = typeof(NavigationContainer);

            // we also indicate the default state
            TransitionToNavigationState(NavigationState.UnNavigated, true);
        }

#endregion

#region Helpers

        protected override void TransitionToNavigationState(NavigationState navigationState)
        {
            this.TransitionToNavigationState(navigationState, true);
        }

        protected virtual void TransitionToNavigationState(NavigationState navigationState, bool useTransitions)
        {
            VisualStateManager.GoToState(this as Control, navigationState.ToString(), useTransitions);
            base.TransitionToNavigationState(navigationState);
        }

//        protected override void ShowFailedNavigationState(NavigationRequest request, ResponseStatus status, Exception error)
//        {
//#if SILVERLIGHT
//            // we show an error thing, and return to the navigated state
//            this.Dispatcher.BeginInvoke(() =>
//            {
//                if (!Debugger.IsAttached)
//                {
//                    MessageBox.Show(string.Format(NAVIGATION_FAILED_INFO, request.Url), NAVIGATION_FAILED_TITLE, MessageBoxButton.OK);
//                }
//                TransitionToNavigationState(NavigationState.Navigated, true);
//            });
//#else
//                        this.Dispatcher.BeginInvoke(new Action(() =>
//                        {
//                            if (!Debugger.IsAttached)
//                            {
//                                MessageBox.Show(string.Format(NAVIGATION_FAILED_INFO, request.Url), NAVIGATION_FAILED_TITLE, MessageBoxButton.OK);
//                            }
//                            TransitionToNavigationState(NavigationState.Navigated, true);
//                        }), 
//                        DispatcherPriority.Normal, null);
//#endif
//        }

#endregion

    }
}


